/*
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <algorithm>
#include <cstring>
#include <fcntl.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <pthread.h>
#include <arpa/inet.h>

#include "camera_kit.h"
#include "recorder.h"
#include "securec.h"

#include "camera_sample.h"
#include "rtsp_server.h"
#include "rtp.h"
#include "rtsp_log.h"

static const int32_t g_rtspPort = 8554;
RtspServer rtspServer(g_rtspPort);	

static void ThreadSendVideoStream(Recorder *recorder)
{
	rtspServer.RtspServerSendVidoeStream(recorder);
}

static void ThreadEventLoop(void)
{
	while (1) {
		int newClientNum = 0;
		sleep(1);
		newClientNum = rtspServer.RtspServerAccept();
		if (newClientNum < 0) {
			SAMPLE_INFO("wait accept");	
			continue;
		}
		rtspServer.RtspServerScheduleConnection(newClientNum);	
	}
}


int main()
{
	CameraKit* camKit = CameraKit::GetInstance();
	if (camKit == nullptr) {
        SAMPLE_ERROR("Can not get CameraKit instance");
        return -1;
    }
    list<string> camList = camKit->GetCameraIds();
    string camId;
    
    for (auto &cam : camList) {
        cout << "############camera name:" << cam << endl;
        const CameraAbility *ability = camKit->GetCameraAbility(cam);
        /* find camera which fits user's ability */
        list<CameraPicSize> sizeList = ability->GetSupportedSizes(0);
        for (auto &pic : sizeList) {
            if (pic.width == 1920 && pic.height == 1080) {
                camId = cam;
                break;
            }
        }
    }
    if (camId.empty()) {
        SAMPLE_ERROR("No available camera.(1080p wanted)");
        return -1;
    }

	EventHandler eventHdlr; // Create a thread to handle callback events
    SampleCameraStateMng CamStateMng(eventHdlr);

    camKit->CreateCamera(camId, CamStateMng, eventHdlr);
	sleep(6);

	if (rtspServer.RtspServerSocket() < 0) {
	    SAMPLE_ERROR("RtspServerSocket");
        return -1;
    }

	rtspServer.RtspServerPrintInfo();
	rtspServer.rtspSendVideoStream_ = std::thread(ThreadSendVideoStream, CamStateMng.recorder_);
	rtspServer.rtspLoopShedule_ = std::thread(ThreadEventLoop);
	
	CamStateMng.StartRecord();
	while(1){
		sleep(1);
	};
    return 0;
}

